package org.FaPe3D.core.parsers.casting
{
	
	import org.FaPe3D.engine.cameras.*;
	import org.FaPe3D.engine.elements.*;
	import org.FaPe3D.engine.elements.lights.*;
	import org.FaPe3D.engine.materials.*;
	import org.FaPe3D.engine.materials.utils.*;
	import org.FaPe3D.engine.modifiers.*;
	import org.FaPe3D.engine.render.*;
	import org.FaPe3D.engine.scenes.*;
	import org.FaPe3D.utils.*;
	import org.papervision3d.objects.*;
	
	
	public class Elements
	{
		public function cast(type:String,properties:Object = null,events:Object = null,material:Object = null,param:Object = null,materialParam:Object = null, id:String = ''):*
		{
			var result:*;
			var m;
			if(!param)
			{
				param = new Object();
			}
			
			if(!materialParam)
			{
				materialParam = new Object();
			}
			
			switch (type)
			{
				case 'Viewport3D':
					result = new FPViewport3D(InitialValues.check(param.viewportWidth,640),
											  InitialValues.check(param.viewportHeight,480),
											  InitialValues.check(param.autoScaleToStage,false),
											  InitialValues.check(param.interactive,false),
											  InitialValues.check(param.autoCliping,true),
											  InitialValues.check(param.autoCulling,true));
				break;
				case 'Camera3D':
					result = new FPCamera3D(InitialValues.check(param.target,null),
											InitialValues.check(param.zoom,11),
											InitialValues.check(param.focus,100));
										 
				break;
				case 'FreeCamera3D':
					result = new FPFreeCamera3D(InitialValues.check(param.zoom,11),
												InitialValues.check(param.focus,100));
				break;
				case 'Scene3D':
					result = new FPScene3D();
				break;
				case 'BasicRenderEngine':
					result = new FPBasicRenderEngine();
				break;
				case 'Plane':
						
					result = new FPPlane(InitialValues.check(param.material,new FPColorMaterial()),
										 InitialValues.check(param.width,100),
										 InitialValues.check(param.height,100),
										 InitialValues.check(param.segmentsW,1),
										 InitialValues.check(param.segmentsH,1));
				break;
				case 'Cube':
				
					var matList:FPMaterialsList = new FPMaterialsList();
					if(material)
					{
						var material_caster:Materials = new Materials();
						materialParam = RegisterMaterials.checkParam(materialParam);
						for (m in material)
						{
							materialParam[m] = RegisterMaterials.checkParam(materialParam[m]);
							matList.addMaterial(material_caster.cast(material[m],materialParam[m]),material[m].name);
							//element.getMaterialByName(materials[m].name).copy(material_caster.cast(materials[m],param[m]));
						}
					}
					else
					{
						matList.addMaterial(new FPColorMaterial(),'front');
						matList.addMaterial(new FPColorMaterial(),'back');
						matList.addMaterial(new FPColorMaterial(),'left');
						matList.addMaterial(new FPColorMaterial(),'right');
						matList.addMaterial(new FPColorMaterial(),'top');
						matList.addMaterial(new FPColorMaterial(),'bottom'); 
					}
					
					result = new FPCube(matList,
										InitialValues.check(param.width,100),
										InitialValues.check(param.depth,100),
										InitialValues.check(param.height,100),
										InitialValues.check(param.segmentsS,1),
										InitialValues.check(param.segmentsT,1),
										InitialValues.check(param.segmentsH,1),
										InitialValues.check(param.insideFaces,0),
										InitialValues.check(param.excludeFaces,0));
				break;
				case 'MagicalCube':
					result = new FPMagicalCube();
				break;
				case 'Cylinder':
					result = new FPCylinder(InitialValues.check(param.material,new FPColorMaterial()),
											InitialValues.check(param.radius,100),
											InitialValues.check(param.height,100),
											InitialValues.check(param.segmentsW,8),
											InitialValues.check(param.segmentsH,8),
											InitialValues.check(param.topRadius,100),
											InitialValues.check(param.topFace,true),
											InitialValues.check(param.bottomFace,true));
				break;
				case 'DisplayObject3D':
					result = new DisplayObject3D();
				break;
				case 'PointLight3D':
					result = new FPPointLight3D(InitialValues.check(param.showLight,false),
												InitialValues.check(param.flipped,false));
				break;
				case 'Collada':
				
					matList = new FPMaterialsList();
					if(material)
					{
						material_caster = new Materials();
						materialParam = RegisterMaterials.checkParam(materialParam);
						for (m in material)
						{
							materialParam[m] = RegisterMaterials.checkParam(materialParam[m]);
							matList.addMaterial(material_caster.cast(material[m],materialParam[m]),material[m].name);
							//element.getMaterialByName(materials[m].name).copy(material_caster.cast(materials[m],param[m]));
						}
					}
					else
					{
						matList = null;
					}
				
				
					result = new FPCollada(InitialValues.check(param.COLLADA,''),
										   matList,
										   InitialValues.check(param.scale,1),
										   InitialValues.check(param.doubleSided,true),
										   InitialValues.check(param.initObject,null));
				break;
			}
			RegisterProperties.register(result,properties);
			RegisterMaterials.register(result,material,materialParam, id);
			return result;
		}
	}
}